package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.util.Tool;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserImpl();


    /**
     * 1.验证码是否正确
     * 2.如果不正确，则返回验证码不正确
     * 3.如果正确，则调用dao层查询数据库
     * 4.如果根据用户名和密码查询出有数据，则可以登录
     * 5.否则不能登录
     * 伪代码
     *
     * @param userModel
     * @return
     */
    @Override
    public ResultModel login(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
        String msg = "";
        if (userModel.getCode().equals(userModel.getSessionCode())) {
//            resultModel.setMsg("success");
//            调用数据实现层，根据用户名和密码去查询数据库，查看是否有值，
//            根据不同的返回值判断用户名或者密码是否正确
            UserDao userDao = new UserImpl();
//            如果新增用户的时候已经对密码加密了，
//            那么在登录时，必须对密码加密后在和数据库的密码进行对比
//            密码 暗文  明文
            userModel.setPassword(Tool.encodeByMD5(userModel.getPassword()));
            UserModel model = userDao.findByUsernameAndPassword(userModel);//从数据库里面查询数据
            if (model == null) {
                msg = "error";
            } else {
                msg = "success";
            }
        } else {
            msg = "error-code";
        }

        return ResultModel.getResultModel(msg);
    }

    @Override
    public ResultModel findAll(UserModel userModel) {
//        UserDao userDao = new UserImpl();
//        findById(1);一条数据就是一个model，那么无数条数据呢？
//        List<UserModel> list = userDao.findAll(userModel);
//        List<UserModel> userModelList = new ArrayList<>();
//        for (int i = 0; i < list.size(); i++) {
//            UserModel model = list.get(i);
////            2022-11-22 00:00:00
//            model.setBirthday(list.get(i).getBirthday().split(" ")[0]);
//            userModelList.add(model);
//        }

//        ResultModel resultModel = new ResultModel();
//        int count = userDao.getCount(userModel);
//        resultModel.setCode(0);
//        resultModel.setCount(count);
//        resultModel.setData(list);
        return ResultModel.getResultModel(userDao.findAll(userModel), userDao.getCount(userModel));
    }

    @Override
    public ResultModel getCount(UserModel userModel) {
        return null;
    }

    @Override
    public ResultModel updateSex(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
////        resultModel.setCode(0);
//        UserDao userDao = new UserImpl();
//        int count = userDao.updateSex(userModel);
//        resultModel.setCount(count);
//        return resultModel;
        return ResultModel.getResultModel(userDao.updateSex(userModel));
    }

    @Override
    public ResultModel updateEnable(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
//        UserDao userDao = new UserImpl();
//        int count = userDao.updateEnable(userModel);
//        resultModel.setCount(count);
//        return resultModel;
        return ResultModel.getResultModel(userDao.updateEnable(userModel));
    }

    @Override
    public ResultModel add(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
        String mingwen = userModel.getPassword();
        String anwen = Tool.encodeByMD5(mingwen);
        userModel.setPassword(anwen);
////        userModel.setPassword(encodeByMD5(userModel.getPassword()));
//        UserDao userDao = new UserImpl();
//        int count = userDao.add(userModel);
//        resultModel.setCount(count);
//        return resultModel;
        return ResultModel.getResultModel(userDao.add(userModel));
    }

    @Override
    public ResultModel findById(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
//        UserDao userDao = new UserImpl();
//        UserModel model=userDao.findById(userModel);
//        resultModel.setData(model);
//        return resultModel;
        return ResultModel.getResultModel(userDao.findById(userModel));
    }

    @Override
    public ResultModel update(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
//        UserDao userDao = new UserImpl();
//        int count = userDao.update(userModel);
//        resultModel.setCount(count);
//        return resultModel;
        return ResultModel.getResultModel(userDao.update(userModel));
    }

    @Override
    public ResultModel delete(UserModel userModel) {
        return ResultModel.getResultModel(userDao.delete(userModel));
    }




}
